/**
  ETFRoot project, Anyang Normal University && IMP-CAS
  \brief unpacker for raw data collected by PXI daq
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2021-09-27
  \date 2021-09-27 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date        <th>Author      <th>Description                    </tr>
  <tr>  <td>2021-09-27   <td>Asia Sun    <td>file created                   </tr>
  </table>

  \copyright Copyright (c) 2021 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

#ifndef ETFPXIUnpacker_h
#define ETFPXIUnpacker_h

#include "ETFUnpacker.h"

class ETFPXISection;
struct fragment_head;

class ETFPXIUnpacker : public ETFUnpacker{
public:
  ETFPXIUnpacker(); ///< type=0 is used to indicate PXI daq
  virtual ~ETFPXIUnpacker();

  virtual bool Init(); ///< register the raw data item array
  virtual void Reset(); ///< clear the output before each event loop
  /// do the unpack. \param nev: number of events to be analysed
  virtual bool DoUnpack(Int_t *data, Int_t nev){ return true; }
  /// \retval: return index of the event being read
  virtual int GetOneEvent(FILE *file); ///< get 1 event to memory from file
  /// \retval: return index of the event being read
  virtual int GetOneEvent(int *fstream); ///< get 1 event to memory from stream
  virtual bool Unpack(); ///< complete the unpacking to clones arrays
  const ETFPXISection *GetCurrentEvent() const{ return fPXISection; }
  virtual void PrintSummary(); ///< print unpacking summary

  void SetIsCheckBunchID(bool opt){ fIsCheckBunchID = opt; }

  ClassDef(ETFPXIUnpacker, 0); // unpacker for PXI raw data

protected:
  virtual void Register(); ///< register the raw data to the output tree as branches

  fragment_head *fFrag_h;
  int *fData; ///< to buffer data of a section
  ETFPXISection *fPXISection; ///< one section is an event

  int fBunchIDMisAlignCnt; ///< count for fragments' bunchids' inconsistencies
  bool fIsCheckBunchID; ///< print out section info if a bunchId-misalignment happens
};

#endif
